package cn.hdu.liang.ch4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static cn.hdu.liang.ch4.Dish.menu;

public class TwoImplement {
    public static void main(String[] args) {
        oldImpl();
        newImpl();
    }

    public static void oldImpl() {
        List<Dish> lowCaDishes = new ArrayList<>();
        for(Dish dish : menu) {
            if(dish.getCalories() < 400) {
                lowCaDishes.add(dish);
            }
        }

        Collections.sort(lowCaDishes, new Comparator<Dish>() {
            @Override
            public int compare(Dish o1, Dish o2) {
                return Integer.compare(o1.getCalories(), o2.getCalories());
            }
        });

        List<String> lowCaDishesName = new ArrayList<>();
        for(Dish dish : lowCaDishes) {
            lowCaDishesName.add(dish.getName());
        }

        System.out.println(lowCaDishesName);
    }

    public static void newImpl() {
        List<String> lowCaDishesName = menu.parallelStream()
                .filter(d -> d.getCalories() < 400)
                .sorted(Comparator.comparing(Dish::getCalories))
                .map(Dish::getName)
                .collect(Collectors.toList());
        System.out.println(lowCaDishesName);
    }
}
